-
-
Notifications
You must be signed in to change notification settings - Fork 284
Add new Rails/JSONSymbolizeNames cop
#1534
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Add new Rails/JSONSymbolizeNames cop
#1534
Conversation
5d3d3cd to
d75d110
Compare
|
Could you first propose this style to the style guide? |
| # JSON.parse(json, symbolize_names: true) | ||
| # | ||
| class JSONSymbolizeNames < Base | ||
| MSG = 'Use `symbolize_names` option.' |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This message only shows the option name, which might make it difficult for users to understand the expected action in their code. It may be simpler to provide autocorrection and show the corrected code instead.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I've implemented autocorrection
…ep_symbolize_keys` ref: rubocop/rubocop-rails#1534
fb6b1c1 to
62d7df0
Compare
From the issue:
So seems like this cop is acceptable, could you please take a look? @koic |
62d7df0 to
5622ae7
Compare
5622ae7 to
19e40c1
Compare
I've seen this pattern a lot: ```ruby JSON.parse(large_json).deep_symbolize_keys ``` instead of travesing Ruby hash once more we can use the `symbolize_names` option: ```ruby JSON.parse(large_json, symbolize_names: true) ``` Caveats / FP scenarios: 1. `symbolize_names` does not work if `create_addition` option is provided. 2. User might use both `symbolize_names: false` and `deep_symbolize_keys`. 3. There's no autocorrection yet, but it should be easy to add.
19e40c1 to
750fbc7
Compare
I've seen this pattern a lot:
instead of travesing Ruby hash once more we can use the
symbolize_namesoption:Caveats / FP scenarios:
symbolize_namesdoes not work ifcreate_additionoption is provided.symbolize_names: falseanddeep_symbolize_keys.Before submitting the PR make sure the following are checked:
master(if not - rebase it).bundle exec rake default. It executes all tests and runs RuboCop on its own code.{change_type}_{change_description}.mdif the new code introduces user-observable changes. See changelog entry format for details.